package org.crazyit.activiti_d5._4_QueryGroupsByUser;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.Group;
import org.crazyit.activiti_d3._3_UpdateUser.UpdateUser;
import org.crazyit.activiti_d5._3_QueryUsersByGroup.QueryUsersByGroup;

import java.util.List;
import java.util.UUID;

public class QueryGroupsByUser {

    public static void main(String[] args) {

        //1. 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        //2. 得到身份服务组件实例
        IdentityService identityService = engine.getIdentityService();

        //3. 创建一个用户
        String userId = UUID.randomUUID().toString();
        UpdateUser.createUser(identityService,userId,"张三","young","yangenxiong@163.com","abc");

        //4. 创建两个用户组
        Group group1 = QueryUsersByGroup.createGroup(identityService, UUID.randomUUID().toString(), "经理组", "manager");
        Group group2 = QueryUsersByGroup.createGroup(identityService, UUID.randomUUID().toString(), "员工组", "manager");

        //5. 将用户与两个用户组板顶关系
        identityService.createMembership(userId, group1.getId());
        identityService.createMembership(userId, group2.getId());

        //6. 调用memberOfGroup 方法查询用户所在的用户组
        List<Group> groups = identityService.createGroupQuery().groupMember(userId).list();
        System.out.println("张三属于的用户组有:");
        for (Group group : groups) {
            System.out.println(group.getName());
        }

    }

}
